#include <err.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>

#include <pkg.h>
#include <pkg_freebsd.h>

int
main(int argc, char **argv)
{
        char *buf;
        struct pkg *p;
        int r;
        struct stat sb;
        int f;

        for (int i = 1; i < argc; ++i) {
                stat(argv[i], &sb);
                buf = calloc(1, sb.st_size + 1);

                f = open(argv[i], O_RDONLY);
                if (f < 0) {
                        errx(1, "Could not open %s", argv[i]);
                }

                read(f, buf, sb.st_size);
                buf[sb.st_size] = '\0';

                close(f);

                p = pkg_freebsd_create();

                printf("parsing: %s\n", argv[i]);

                r = pkg_parse(p, buf, "CONTENTS");
                if (r != PKG_OK) {
                        printf("fail\n");
                }

                printf("Parse complete.\n");

                printf("PkgName: %s\n", pkg_freebsd_get_name(p));

        }

        return (0);
}
